package app.netant.book.ibook8;

import org.jsoup.select.Elements;

import app.netant.book.base.BaseDetail;
/**
 * 
 * @author JiaJ
 *
 */
public class IBook8Detail extends BaseDetail {

	public IBook8Detail(String netSite) {
		super(netSite);
		this.preHref = "http://www.ibook8.com";
	}

	@Override
	public void analyseTitle() {
		Elements parts = this.select(".headerbox #pageMain #mainBody .pageMainArea dl#downInfoArea dd.downInfoRowL a font b");
		if(parts.size()>0){
			String title = parts.get(0).text();
			System.out.println("【Title】:"+title);
			this.entity.setNovelName(title);
			this.entity.setNovelFullName(title+".rar");
		}
	}

	@Override
	public void analyseTags() {
		Elements parts = this.select(".headerbox #pageMain #mainBody .pageMainArea #downInfoArea .downInfoRowL");
		if(parts.size()>0){
			String tags = parts.get(0).text();
			this.splitTags(tags, true);
		}
	}

	@Override
	public void analyseMemo() {
		Elements parts = this.select(".headerbox #pageMain #mainBody .pageMainArea .mainstory .story #mainSoftIntro p");
		if(parts.size()>0){
			String memo = parts.get(0).text();
			System.out.println("【Memo】:"+memo);
			this.entity.setNovelMemo(memo);
		}
	}

	@Override
	public void analyseHref() {
		Elements parts = this.select(".headerbox #pageMain #mainBody .pageMainArea #downibook8 #downlinks .odlinks .thelinks a");
		for(int i=0;i<parts.size();i++){
			String txt = parts.get(i).text();
			if(txt.indexOf("下载")>=0){
				String url = parts.get(i).attr("href");
				
				if(url.startsWith("/")){
					url = this.preHref + url;
				}
				
				System.out.println("【Url】:"+url);
				
				IBook8Detail detail = new IBook8Detail(url);
				String href = detail.stepHref();
				System.out.println("【Href】："+href);
				
				this.entity.setDownPath(href);
				
				break;
			}
		}
	}
	
	/**
	 * 链接处理
	 * @return
	 */
	public String stepHref(){
		
		Elements parts = this.select("table tbody tr td table tbody tr td table tbody tr td a");
		
		for(int i=0;i<parts.size();i++){
			String txt = parts.get(i).text();
			if(txt.indexOf("普通下载")>=0){
				String href = parts.get(i).attr("href");
				return href;
			}
		}
		
		return null;
		
	}
	
	@Override
	public void analyseType() {
		Elements parts = this.select(".headerbox #pageMain .crumb .crumbleft a");
		if(parts.size()>2){
			String type = parts.get(2).text();
			System.out.println("【Type】:"+type);
			this.entity.setNovelType(type);
		}
	}
	
	public static void main(String[] args) {
		IBook8Detail detail = new IBook8Detail("http://www.ibook8.com/xuanhuan/xh/27623.html");
		detail.test();
	}



}
